package com.ruoyi.system.controller.trade;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.common.BillTypeConstant;
import com.ruoyi.system.domain.db.financial.FinancialEconomic;
import com.ruoyi.system.domain.db.trade.TradeVehicleOrder;
import com.ruoyi.system.domain.vo.PageInfo;
import com.ruoyi.system.service.IFinancialEconomicService;
import com.ruoyi.system.utils.MyPageUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.system.service.ITradeVehicleOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 汽车采购Controller
 * 
 * @author ruoyi
 * @date 2023-07-31
 */
@RestController
@RequestMapping("/trade/vehicle")
public class TradeVehicleOrderController extends BaseController
{
    @Autowired
    private ITradeVehicleOrderService tradeVehicleOrderService;

    @Autowired
    private IFinancialEconomicService financialEconomicService;

    /**
     * 查询汽车采购列表
     */
    @PreAuthorize("@ss.hasPermi('trade:vehicle:list')")
    @GetMapping("/list")
    public PageInfo<TradeVehicleOrder> list(TradeVehicleOrder tradeVehicleOrder)
    {
        startPage();
        List<TradeVehicleOrder> list = tradeVehicleOrderService.selectTradeVehicleOrderList(tradeVehicleOrder);
        return MyPageUtils.startPage(list);
    }

    /**
     * 导出汽车采购列表
     */
    @PreAuthorize("@ss.hasPermi('trade:vehicle:export')")
    @Log(title = "汽车采购", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TradeVehicleOrder tradeVehicleOrder)
    {
        List<TradeVehicleOrder> list = tradeVehicleOrderService.selectTradeVehicleOrderList(tradeVehicleOrder);
        ExcelUtil<TradeVehicleOrder> util = new ExcelUtil<TradeVehicleOrder>(TradeVehicleOrder.class);
        util.exportExcel(response, list, "汽车采购数据");
    }

    /**
     * 获取汽车采购详细信息
     */
    @PreAuthorize("@ss.hasPermi('trade:vehicle:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId)
    {
        return success(tradeVehicleOrderService.selectTradeVehicleOrderByOrderId(orderId));
    }

    /**
     * 新增汽车采购
     */
    @PreAuthorize("@ss.hasPermi('trade:vehicle:add')")
    @Log(title = "汽车采购", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TradeVehicleOrder tradeVehicleOrder)
    {
        int result = tradeVehicleOrderService.insertTradeVehicleOrder(tradeVehicleOrder);
        FinancialEconomic financialEconomic = new FinancialEconomic(
                BillTypeConstant.VEHICLE_ORDER,tradeVehicleOrder.getOrderId(),
                tradeVehicleOrder.getTotal()*(-1),tradeVehicleOrder.getRemark() // 备注从remark改过去
        );
        financialEconomicService.insertFinancialEconomic(financialEconomic);
        return toAjax(result);
    }

    /**
     * 修改汽车采购
     */
    @PreAuthorize("@ss.hasPermi('trade:vehicle:edit')")
    @Log(title = "汽车采购", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TradeVehicleOrder tradeVehicleOrder)
    {
        boolean flag = false;
        int result = tradeVehicleOrderService.updateTradeVehicleOrder(tradeVehicleOrder);
        FinancialEconomic financialEconomic = financialEconomicService.getBillByOrderIdAndBillTypeId(tradeVehicleOrder.getOrderId(), BillTypeConstant.VEHICLE_ORDER);
        if (!financialEconomic.getSalary().equals(tradeVehicleOrder.getTotal()*(-1))) {
            financialEconomic.setSalary(tradeVehicleOrder.getTotal()*(-1));
            flag = true;
        }
        if (financialEconomic.getInfo() == null) {
            financialEconomic.setInfo("");
        }
        if (!financialEconomic.getInfo().equals(tradeVehicleOrder.getRemark())) {
            financialEconomic.setInfo(tradeVehicleOrder.getRemark());
            flag = true;
        }
        if (flag) {  // 变了就改，不然不改
            financialEconomicService.updateFinancialEconomic(financialEconomic);
        }
        return toAjax(result);
    }

    /**
     * 删除汽车采购
     */
    @PreAuthorize("@ss.hasPermi('trade:vehicle:remove')")
    @Log(title = "汽车采购", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds)
    {
        List<Long> orderIdList = financialEconomicService.arrayToList(orderIds);
        List<Long> billIds = new ArrayList<>();
        for (Long orderId : orderIdList) {
            Long billId = financialEconomicService.getBillByOrderIdAndBillTypeId(orderId, BillTypeConstant.VEHICLE_ORDER).getBillId();
            billIds.add(billId);
        }
        financialEconomicService.deleteFinancialEconomicByOrderIds(billIds);
        return toAjax(tradeVehicleOrderService.deleteTradeVehicleOrderByOrderIds(orderIds));
    }
}
